/**
 * 
 */
//是否登录，默认为否
var logined=false;
Request = {
	query: function(item){
		var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
		return svalue ? svalue[1] : svalue;
	}
};
function set_window(){
	if (typeof set_window_p!=='undefined'&&typeof set_window_p.full!='undefined'&&set_window_p.full===true) full=true;else full=false;
	var win_status=get_win_status();
	var w_w=full?win_status.w_w:win_status.w_w-10,w_h=full?win_status.w_h:win_status.w_h-10;
	if (w_w<400){
		w_w=400;
	}
	if (w_h<300){
		w_h=300;
	}
	$('#content_ctn,#login_ctn').css({'height':w_h});
	if (typeof set_window_p!=='undefined'&&typeof set_window_p.h!='undefined'&&set_window_p.h instanceof jQuery&&set_window_p.h.length>=0)set_window_p.h.css({'width':w_w});
	if (typeof set_window_p!=='undefined'&&typeof set_window_p.v!='undefined'&&set_window_p.v instanceof jQuery&&set_window_p.v.length>=0)set_window_p.v.css({'height':w_h});
	$('#page_ctn').css({'height':w_h,'width':w_w});
}

function show_login(){
	obj=$('#login_ctn');
	if (logined){
		return false;
	}
	if (typeof obj=='string'||typeof obj=='undefined'||!obj instanceof jQuery||obj.length!=1)return false;
	var w_w=$(window).width(),w_h=$(window).height(),d_w=obj.width(),d_h=obj.height();
	var d_l=parseInt((w_w-d_w)/2),d_t=parseInt((w_h-d_h)/2);
	obj.fadeIn();
}
function show_loading(x,y){
	this.random_char=function(l) {
		if (typeof l=='undefined')l=5;
	    var s = "0123456789qwertyuioplkjhgfdsazxcvbnm";
	    var tmp = "";
	    for (var i = 0; i < l; i++) {
	        tmp += s.charAt(Math.ceil(Math.random() * 100000000) % s.length);
	    }
	    return tmp;
	};
	if (typeof x=='undefined'||typeof y=='undefined'){
		x=parseInt(($(window).width()-35)/2);
		y=parseInt(($(window).height()-35)/2);
	}
	this.loader_id=this.random_char(5);
	$('body').append('<div id="'+this.loader_id+'" class="load_animate"></div>');
	var run_img;
	this.runder=function(){
		if (typeof this.loader_id=='undefined')return false;
		var obj=$('#'+this.loader_id);
		if (document.all && !window.opera){
			var b_t=parseInt(obj.css('backgroundPositionY').replace(/px/, ""))-35;
			obj.css({'backgroundPositionY':b_t+'px',top:y,left:x});
		}else{
			var b_t=parseInt(obj.css('backgroundPosition').split(' ')[1].replace(/px/, ""))-35;
			obj.css({'backgroundPosition':'0 '+b_t+'px',top:y,left:x});
		}
	};
	this.run=function(){run_img=window.setInterval($.proxy(this.runder,this),100);};
	this.stop=function(){window.clearInterval(run_img);$('#'+this.loader_id).hide();};
	this.run();
}
function login(p){
	if (logined)return true;

	this.obj=$('#login_ctn');
	this.init=function(){
		if (!this.obj instanceof jQuery||this.obj.length==0){
			$('#page_ctn').append('<div id="login_ctn"><div class="login"><div class="form">'+
				'<label>请输入您的真实姓名：<br/><i class="tip">以确定访问权限</i></label><input value="wong" class="name" name="guest_name" /><button type="submit"></button>'+
				'</div></div></div>');
			this.obj=$('#login_ctn');
			this.obj.height($(window).height()-10);
		}
	};
	this.show=function(){
		this.obj.fadeIn();
	};
	this.init();
	this.show();
	this.obj.find('button').click(function(){
		var load=new show_loading();
		var name=$(this).siblings('input').val();
		if (name==''){
			show_info('请输入用户名','notice',3);
			return false;
		}
		$(this).attr('disabled','disabled').addClass('loading');
		$.get('/ajax_response.php',{type:'login',name:name},function(data){
			if (data.result){
				$('#login_ctn').fadeOut();
				$('#login_ctn button').removeAttr('disabled');
				logined=true;
				load.stop();
				$('#login_bg').fadeOut();
				load_func();
			}else{
				show_info('登录失败','notice',3);
				load.stop();
				$('#login_ctn button').removeAttr('disabled');
			}
		},'json');
		return false;
	});
}
function callback(o,n){
	$('.img_list li').eq(n).removeClass('loading').find('img').css({'margin-top':(120-o.height)/2+'px','margin-left':(160-o.width)/2+'px'});
}
function water_fall(obj){
	this.page_obj=$('#pagination_ctn');
	this.page=1;
	this.page_size=12;
	if (obj.length==0)return false;
	var status=new Object();
	var obj_status=get_obj_status(obj.eq(0));
	var p_obj=obj.parents('#content_ctn');
	p_obj.css('position','relative');
	obj.css('position','absolute');
	this.show=function(fly){
		if (typeof fly=='undefined')fly=false;
		var cur_pos=0;
		for (i=status.start;i<=status.end;i++){
			var cur_col=cur_pos%status.c;
			var cur_row=Math.floor(cur_pos/status.c)+1;
			if (fly){
				obj.eq(i).stop().css({left:status.p_os.w,top:0}).animate({
					left: (status.mh+status.os.w)*cur_col+status.mch,
					top: (status.mv+status.os.h)*(cur_row-1)+status.mcv
				}, 200).show();
			}else{
				obj.eq(i).stop().show().css({left:(status.mh+status.os.w)*cur_col+status.mch,top:(status.mv+status.os.h)*(cur_row-1)+status.mcv});
			}
			cur_pos++;
		}
	};
	this.init=function(){
		var p_obj_status=get_obj_status(p_obj);
		var max_margin_h=parseInt(obj_status.w/4);
		var max_margin_v=parseInt(obj_status.h/4);
		var min_margin_h=parseInt(obj_status.w/6);
		var min_margin_v=parseInt(obj_status.h/6);
		var margin_ctn_h=max_margin_h;
		var margin_ctn_v=max_margin_v;
		var margin_h=(max_margin_h+min_margin_h)/2;
		var col=Math.floor((p_obj_status.w-margin_ctn_h*2+margin_h)/(obj_status.w+margin_h));
		margin_h=Math.floor((p_obj_status.w-margin_ctn_h*2-col*obj_status.w)/(col-1));
		margin_v=Math.floor((max_margin_v+min_margin_v)/2);
		if (margin_h>2*margin_ctn_h)margin_h=2*margin_ctn_h;
		if (margin_h>2*margin_v)margin_v=margin_h/2;
		var total_width=obj_status.w*col+margin_h*(col-1);
		margin_ctn_h=(p_obj_status.w-total_width)/2;
		this.valid_h=p_obj_status.h-margin_ctn_v-43;
		this.page_row=Math.floor(this.valid_h/(margin_v+obj_status.h));
		this.page_size=this.page_row*col;
		this.page_count=Math.ceil(obj.length/this.page_size);
		status.c=col;
		status.pr=this.page_row;
		status.ps=this.page_size;
		status.valid_h=p_obj_status.h-margin_ctn_v-43;
		status.os=obj_status;
		status.mch=margin_ctn_h;
		status.mcv=margin_ctn_v;
		status.mh=margin_h;
		status.mv=margin_v;
		status.p_os=p_obj_status;
		if (typeof status.start=='undefined'||status.start<0)status.start=0;
		status.end=status.start+this.page_size-1;
		if (status.start>0&&this.page>1){
			this.page=Math.ceil(status.start/this.page_size)+1;
		}
		obj.hide();
		this.show();
		this.show_page();
	};
	
//	console.log(margin_h);
//	console.log(total_width);
	//p_obj.css({'position':'relative','width':total_width,'height':p_obj_status.h-2*margin_v,'padding':margin_ctn_v+'px '+margin_ctn_h+'px'});
	//有效内容高度
//	this.margin_ctn_h=margin_ctn_h;
//	this.margin_ctn_v=margin_ctn_v;
//	this.obj_status=obj_status;
//	this.margin_h=margin_h;
//	this.margin_v=margin_v;
//	this.col=col;
	this.flip_page=function(p){
		if (typeof p=='undefined')p=1;
		status.start=this.page_size*(p-1);
		status.end =status.start+this.page_size-1;
		this.page=p;
		p_obj.find('ul li:visible').each(function(){
			$(this).stop().animate({
				left: -status.os.w,
				top: -status.os.h
			},200,function(){$(this).hide();});
		});
		this.show(true);
	};
	this.show_page=function(){
		if (this.page_obj.length==0)return false;
		var s='';
		if (this.page_count>1||this.page>1)
			for (i=1;i<=this.page_count;i++){
				s+='<a href="javascript:void(0)"'+(i==this.page?' class="c"':'')+'></a>';
			}
		this.page_obj.html(s);
	};
	this.init();
}
var get_obj_status=function(d,w){
	var val=new Object();
	if (typeof d=='undefined')return false;
	if (typeof w=='undefined')w=false;
	val.w=d.width();
	if (typeof d.css('padding-left')!='undefined'&&d.css('padding-left').indexOf('px')>0){
		val.pl=parseInt(d.css('padding-left').replace(/px/, ""));
		val.w+=parseInt(d.css('padding-left').replace(/px/, ""));
	}
	if (typeof d.css('padding-right')!='undefined'&&d.css('padding-right').indexOf('px')>0){
		val.w+=parseInt(d.css('padding-right').replace(/px/, ""));
		val.pr=parseInt(d.css('padding-right').replace(/px/, ""));
	}
	if (typeof d.css('border-left-width')!='undefined'&&d.css('border-left-width').indexOf('px')>0)
		val.w+=parseInt(d.css('border-left-width').replace(/px/, ""));
	if (typeof d.css('border-right-width')!='undefined'&&d.css('border-right-width').indexOf('px')>0)
		val.w+=parseInt(d.css('border-right-width').replace(/px/, ""));
	val.h=d.height();
	if (typeof d.css('padding-top')!='undefined'&&d.css('padding-top').indexOf('px')>0){
		val.h+=parseInt(d.css('padding-top').replace(/px/, ""));
		val.pt=parseInt(d.css('padding-top').replace(/px/, ""));
	}
	if (typeof d.css('padding-bottom')!='undefined'&&d.css('padding-bottom').indexOf('px')>0){
		val.h+=parseInt(d.css('padding-bottom').replace(/px/, ""));
		val.pb=parseInt(d.css('padding-bottom').replace(/px/, ""));
	}
	if (typeof d.css('border-top-width')!='undefined'&&d.css('border-top-width').indexOf('px')>0)
		val.h+=parseInt(d.css('border-top-width').replace(/px/, ""));
	if (typeof d.css('border-bottom-width')!='undefined'&&d.css('border-bottom-width').indexOf('px')>0)
		val.h+=parseInt(d.css('border-bottom-width').replace(/px/, ""));
	if (typeof d.css('margin-left')!='undefined'&&d.css('margin-left').indexOf('px')>0){
		val.ml=parseInt(d.css('margin-left').replace(/px/, ""));
		val.w+=parseInt(d.css('margin-left').replace(/px/, ""));
	}
	if (typeof d.css('margin-right')!='undefined'&&d.css('margin-right').indexOf('px')>0){
		val.mr=parseInt(d.css('margin-right').replace(/px/, ""));
		val.w+=parseInt(d.css('margin-right').replace(/px/, ""));
	}
	if (typeof d.css('margin-top')!='undefined'&&d.css('margin-top').indexOf('px')>0){
		val.mt=parseInt(d.css('margin-top').replace(/px/, ""));
		val.h+=parseInt(d.css('margin-top').replace(/px/, ""));
	}
	if (typeof d.css('margin-bottom')!='undefined'&&d.css('margin-bottom').indexOf('px')>0){
		val.mb=parseInt(d.css('margin-bottom').replace(/px/, ""));
		val.h+=parseInt(d.css('margin-bottom').replace(/px/, ""));
	}
	if (w){
		var w_s=get_win_status();
		val.st=w_s.st;
		val.sl=w_s.sl;
		val.w_h=w_s.w_h;
		val.w_w=w_s.w_w;
	}
	return val;
};
var get_win_status=function(){
	var val=new Object();
	val.st=$(document).scrollTop();
	val.sl=$(document).scrollLeft();
	val.w_h=$(window).height();
	val.w_w=$(window).width();
	return val;
};
var info_dialog;
function show_info(s,t,d){
	if (typeof s=='undefined')return false;
	if (typeof d=='undefined')d=false;
	if ($('#YY_dialog_ctn_info_dialog').length==0){
		$('body').append('<div id="info_dialog"><p></p></div>');
	}
	$('#info_dialog p').html(s);
	var c=$('#info_dialog');
	if (typeof t!='undefined'&&t!=''){
		c.find('p').attr('class',t);
	}
	c.find('p').html(s);
	if ($('#YY_dialog_ctn_info_dialog').length==0){
		var p_info={restrict:true,content_id:"info_dialog",easy_close:false,force_center:true,auto_close:d};
		info_dialog=new YY_dialog(p_info);
	}else{
		info_dialog.auto_close=d;
		info_dialog.render_dialog();
	}
	info_dialog.show_dialog();
}
function close_info(){
	info_dialog.close_dialog();
}
/**
 * 为一个字符串的固定位置加入分隔符
 * @param s 字符串
 * @param l 分隔符间距，单位字节
 * @param d 分隔符字符串，默认为逗号
 * @author yaoyoa
 */
function str_divide(s,l,d){
	if (typeof s=='undefined')return false;
	if (typeof l=='undefined')l=12;
	if (typeof d=='undefined')d=',';
	var len=0;
	var s_n='';
	for (i=0;i<s.length;i++){
		s_n+=s.substr(i,1);
		len++;
//		console.log(s.substr(i,1).match(/[^\x00-\x80]/g));
		if (s.substr(i,1).match(/[^\x00-\x80]/g)){
			len++;
				
		}
		if (len%l==0){
			s_n+='<br/>';
		}
		if (len%l==l-1&&s.substr(i+1,1).match(/[^\x00-\x80]/g))s_n+='<br/>';
	}
	return s_n;
}
